package com.work.chat.common.util;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 对象复制工具类
 * 特点：赋值方属性为空则不赋值给被赋值方
 */
public class ObjectCopyUtil {

    public static void copyProperties(Object source, Object target) {
        try {
            for (java.beans.PropertyDescriptor propertyDescriptor : java.beans.Introspector.getBeanInfo(source.getClass()).getPropertyDescriptors()) {
                if (!propertyDescriptor.getName().equals("class")) {
                    Object value = BeanUtils.getProperty(source, propertyDescriptor.getName());
                    if (value != null) {
                        BeanUtils.setProperty(target, propertyDescriptor.getName(), value);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
